package com.ght.springboot.service.impl;

import com.ght.springboot.api.form.CommentForm;
import com.ght.springboot.api.query.CommentQuery;
import com.ght.springboot.api.query.ElasticSearchQuery;

import com.ght.springboot.elasticsearch.BaseOperates;
import com.ght.springboot.elasticsearch.domain.BaseMapping;
import com.ght.springboot.elasticsearch.domain.BookCityMapping;
import com.ght.springboot.elasticsearch.domain.FieldAttribute;
import com.ght.springboot.service.EsServcie;
import com.ght.springboot.util.MyEasyJsonUtil;
import com.ght.springboot.vo.CommentBookVo;
import io.searchbox.core.SearchResult;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @description:
 * @author: haitao.gao
 * @create: 2019-02-17 19:31
 **/
@Service
public class EsServiceImpl implements EsServcie {


    @Override
    public String insertComment(CommentForm commentForm) {
        BaseOperates<CommentBookVo> baseOperates = new BaseOperates();
        String json = MyEasyJsonUtil.json2string(commentForm);
        baseOperates.insert("book_city","book_comment",json);
        return "成功";
    }

    @Override
    public List<CommentBookVo> queryComment(CommentQuery queryDto,String index,String type) {

        List<CommentBookVo> result = new ArrayList<>();
        BaseOperates<CommentBookVo> baseOperates = new BaseOperates();
        ElasticSearchQuery<CommentQuery> query = new ElasticSearchQuery();
        query.setIndex(index);
        query.setType(type);
        query.setMust(queryDto);
        List<SearchResult.Hit<JSONObject, Void>> hitList = baseOperates.search(query);
        hitList.forEach(hit->{
            Map<String,String> source = hit.source;
            CommentBookVo blackAddressVo = new CommentBookVo(source);
            result.add(blackAddressVo);
        });
        return result;

    }

    @Override
    public String createIndex() {
        BaseOperates<CommentBookVo> baseOperates = new BaseOperates<>();
        baseOperates.createIndex("book_city","Lyq");
        BookCityMapping bookCityMapping = new BookCityMapping(
                new FieldAttribute("text","english",1),
                new FieldAttribute("date",null,1,"yyyy-MM-dd"),
                new FieldAttribute("text","english",1),
                new FieldAttribute("text","english",1),
                new FieldAttribute("text","english",1),
                new FieldAttribute("text","english",1),
                new FieldAttribute("text","english",1),
                new FieldAttribute("text","english",1),
                new FieldAttribute("text","english",1)
        );
        BaseMapping baseMapping = new BaseMapping(bookCityMapping);
        baseOperates.createIndexBymapping("book_city","Lyq",MyEasyJsonUtil.json2string(baseMapping));
        return "成功";
    }

    public static void main(String[] args) {
        EsServiceImpl esService = new EsServiceImpl();
//        CommentForm commentForm = new CommentForm();
//        commentForm.setBookName("十万个为什么");
//        commentForm.setBookComment("这是一本牛逼的书");
//        commentForm.setInsertTime("2018-12-12");
//        commentForm.setNextBook("更牛逼的十万个为什么");
//        commentForm.setUserName("DY");
//        esService.insertComment(commentForm);
        CommentQuery commentQuery = new CommentQuery();
//        commentQuery.setBookComment("这是");
//        commentQuery.setInsertTime("2018-12-12");
//        esService.queryComment(commentQuery);

    }
}
